import ast
import sys
import traceback

def check_syntax(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        ast.parse(content)
        print("文件语法正确")
    except SyntaxError as e:
        print(f"语法错误: {e}")
        print(f"错误行号: {e.lineno}")
        print(f"错误位置: {e.offset}")
        print(f"错误信息: {e.msg}")
        # 显示错误行附近的内容
        lines = content.split('\n')
        start = max(0, e.lineno - 3)
        end = min(len(lines), e.lineno + 3)
        print("错误行附近内容:")
        for i in range(start, end):
            prefix = '> ' if i + 1 == e.lineno else '  '
            print(f"{prefix}{i+1}: {lines[i]}")
    except Exception as e:
        print(f"其他错误: {e}")
        traceback.print_exc()

if __name__ == "__main__":
    file_path = "d:/未知/yolo_apply/video_processing/video_processor.py"
    check_syntax(file_path)